package com.newtouch.bxzs.database.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.database.config.MyLog;
import com.newtouch.bxzs.database.model.bo.*;
import com.newtouch.bxzs.database.model.vo.*;
import com.newtouch.bxzs.database.service.CooperationCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 合作公司接口Controller类
 *
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/20 17:19</td>
 * <td>田尚柱</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author 田尚柱
 * @version 1.0.0
 */

@Api(tags = "合作公司管理")
@RestController
@RequestMapping("/cooperationCompanyController")
@SuppressWarnings("unchecked")
public class CooperationCompanyController {
    @Resource
    private CooperationCompanyService cooperationCompanyService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 静态变量：日志
     */
    private static final Log logger = LogFactory.getLog(CooperationCompanyController.class);

    @ApiOperation("获取合作公司列表")
    @PostMapping("/getCooperationCompanyList")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/getCooperationCompanyList", userOperationType = 4, userOperationSummary = "获取合作公司列表")
    public BaseResponse<IPage<CooperationCompanyBO>> getCooperationCompanyList(@RequestBody CooperationCompanyVO cooperationCompanyVO) {
        logger.info("开始获取合作公司列表");
        return RespGenerator.returnOK(cooperationCompanyService.getCooperationCompanyList(cooperationCompanyVO));
    }

    @ApiOperation("获取已启用的保险公司")
    @PostMapping("/getInsuranceCompanyEnabled")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/getInsuranceCompanyEnabled", userOperationType = 4, userOperationSummary = "获取已启用的保险公司")
    public BaseResponse<InsuranceCompanyTotalBO> getInsuranceCompanyEnabled(@RequestBody @ApiParam InsuranceCompanyVO insuranceCompanyVO) {
        logger.info("开始获取已启用的保险公司");
        return RespGenerator.returnOK(cooperationCompanyService.getInsuranceCompanyEnabled(insuranceCompanyVO));
    }

    @ApiOperation("根据统一社会信用代码获取合作公司")
    @PostMapping("/getCompanyByCode")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/getCompanyByCode", userOperationType = 4, userOperationSummary = "根据统一社会信用代码获取合作公司")
    public BaseResponse<CooperationCompanyListBO> getCompanyByCode(@RequestBody(required = false) @ApiParam(value = "统一社会信用代码的集合") List<String> codeList) {
        logger.info("根据统一社会信用代码获取合作公司");
        return RespGenerator.returnOK(cooperationCompanyService.getCompanyByCode(codeList));
    }

    @ApiOperation("新增/修改合作公司")
    @PostMapping("/modifyCooperationCompany")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/modifyCooperationCompany", userOperationType = 5, userOperationSummary = "新增/修改合作公司")
    public BaseResponse<String> modifyCooperationCompany(@RequestBody CooperationCompanyModifyVO cooperationCompanyCreateVO) {
        logger.info("新增或者修改合作公司");
        String message = cooperationCompanyService.modifyCooperationCompany(cooperationCompanyCreateVO);
        //选择了启用才加缓存，（禁用才能修改）
        if (cooperationCompanyCreateVO.getEnableFlag()) {
            redisUtils.getAndSet(cooperationCompanyCreateVO.getUnifiedSocialCreditCode(),
                    JSON.toJSONString(cooperationCompanyCreateVO.getCooperationCompanyParameterVOList()));
        }
        return RespGenerator.returnOK(message);
    }

    @ApiOperation("获取合作公司详细信息")
    @PostMapping("/getCooperationCompanyDetail")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/getCooperationCompanyDetail", userOperationType = 4, userOperationSummary = "获取合作公司详细信息")
    public BaseResponse<CooperationCompanyDetailBO> getCooperationCompanyDetail(@RequestBody @ApiParam(value = "合作公司id", required = true) String cooperationCompanyId) {
        logger.info("获取合作公司详细信息");
        return RespGenerator.returnOK(cooperationCompanyService.getCooperationCompanyDetail(cooperationCompanyId));
    }

    @ApiOperation("启用/禁用合作公司")
    @PostMapping("/changeCooperationCompanyStatus")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/changeCooperationCompanyStatus", userOperationType = 4, userOperationSummary = "启用/禁用合作公司")
    public BaseResponse<String> changeCooperationCompanyStatus(
            @RequestBody CooperationCompanyChangeVO cooperationCompanyChangeVO
    ) {
        logger.info("启用或者禁用合作公司");
        String message = cooperationCompanyService.changeCooperationCompanyStatus(cooperationCompanyChangeVO);
        CooperationCompanyDetailBO cooperationCompanyDetail = cooperationCompanyService.getCooperationCompanyDetail(cooperationCompanyChangeVO.getCooperationCompanyId());
        if(BeanUtil.isEmpty(cooperationCompanyDetail)){
            throw new BaseException(BaseErrorEnum.NOT_FOUND);
        }
        String unifiedSocialCreditCode = cooperationCompanyDetail.getUnifiedSocialCreditCode();
        if (cooperationCompanyChangeVO.getEnableFlag()) {
            //启用，加缓存
            redisUtils.getAndSet(unifiedSocialCreditCode, JSON.toJSONString(cooperationCompanyDetail.getCooperationCompanyParameterBOList()));
        } else {
            //禁用，删缓存
            redisUtils.delete(unifiedSocialCreditCode);
        }
        return RespGenerator.returnOK(message);
    }

    @ApiOperation("获取合作公司用户")
    @PostMapping("/getCooperationCompanyUserByCode")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/getCooperationCompanyUserByCode", userOperationType = 4, userOperationSummary = "获取合作公司用户")
    public BaseResponse<List<CooperationCompanyCodeUserBO>> getCooperationCompanyUserByCode(
            @RequestBody CooperationCompanyAllUserVO cooperationCompanyAllUserVO
    ) {
        logger.info("获取合作公司用户");
        return RespGenerator.returnOK(cooperationCompanyService.getCooperationCompanyUserByCode(cooperationCompanyAllUserVO));
    }

    @ApiOperation("获取带首字母的合作公司用户")
    @PostMapping("/getCooperationUserFirstByCode")
    @MyLog(systemFunctionName = "合作公司管理", systemFunctionUrl = "/cooperationCompanyController/getCooperationUserFirstByCode", userOperationType = 4, userOperationSummary = "获取带首字母的合作公司用户")
    public BaseResponse<CooperationUserTotalBO> getCooperationUserFirstByCode(
            @RequestBody CooperationCompanyAllUserVO cooperationCompanyAllUserVO
    ) {
        logger.info("获取带首字母的合作公司用户");
        return RespGenerator.returnOK(cooperationCompanyService.getCooperationUserFirstByCode(cooperationCompanyAllUserVO));
    }
}
